package com.linan.docmanage.controller;

import com.linan.base.domain.BaseDomain;
import com.linan.base.domain.PageList;
import com.linan.docmanage.domain.DocInfo;
import com.linan.docmanage.query.PdfQuery;
import com.linan.docmanage.service.IDocManageService;
import com.linan.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping ("/pdf-manage")
public class DocManagerController {

    @Autowired
    IDocManageService pdfService;

    @PostMapping("/add")
    public AjaxResult addPdf (@RequestBody DocInfo docInfo, HttpServletRequest request){
         Cookie[] cookies = request.getCookies();
        String username=null;
         for (Cookie cookie : cookies) {
             if(cookie.getName().equals("username")){
                 username= cookie.getValue();
             }
         }
        String uuid  = UUID.randomUUID().toString();
        docInfo.setUuId(uuid);
        docInfo.setCreateTime(new Date());
        docInfo.setUpdateTime(new Date());
        docInfo.setCreateBy(username);
        docInfo.setUpdateBy(username);


        pdfService.save(docInfo);



        return new AjaxResult();
    }
    @PostMapping("/update")
    public AjaxResult updatePdf (@RequestBody DocInfo docInfo,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String username=null;
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("username")){
                username= cookie.getValue();
            }
        }
        docInfo.setUpdateTime(new Date());
        docInfo.setUpdateBy(username);
        pdfService.update(docInfo);
        return new AjaxResult();
    }
    @GetMapping("/get-all")
    public AjaxResult queryAll (){
        PdfQuery queryInfo = new PdfQuery();
        PageList<DocInfo> list =pdfService.queryData(queryInfo);
        return new AjaxResult(list);
    }

    @PostMapping("/query-by-fields")
    public  AjaxResult queryByField (@RequestBody PdfQuery pdfQuery){
        final PageList list = pdfService.queryData(pdfQuery);
        return new AjaxResult(list);
    }

    @PostMapping("/delete")
    public AjaxResult delete (@RequestBody BaseDomain baseDomain){
        Long id = baseDomain.getId();
        pdfService.delete(id);
        return new AjaxResult();
    }

    @PostMapping("query-by-uuid")
    public AjaxResult queryByUuid (@RequestBody DocInfo docInfo){
        String uuid = docInfo.getUuId();
        DocInfo info = pdfService.queryByUUid(uuid);
        return new AjaxResult(info);
    }

}
